﻿using System.Collections;
using FairyGUI;

/// <summary>
/// 聊天
/// </summary>
public class UIGameChat : UIPopupWindow {
	
	public UIGameChat()
	{
		// 加载界面
		base.Init(UIPackage.CreateObject("游戏界面", "聊天消息组件").asCom);

		// 初始化关闭按钮
		GButton closeBtn = uiComp.GetChild("关闭按钮").asButton;
		closeBtn.onClick.Add(() => {
			_Close();
		});

		GTextInput msgInput = uiComp.GetChild ("输入").asTextInput;


		GComponent n11 = uiComp.GetChild ("n11").asCom;
		GComponent n0 = n11.GetChild ("n0").asCom;

		// 初始化文字按钮
		GButton msg1Btn = n0.GetChild("b1").asButton;
		msg1Btn.onClick.Add(() => {
			_OnMsg2(1);
		});
		GButton msg2Btn = n0.GetChild("b2").asButton;
		msg2Btn.onClick.Add(() => {
			_OnMsg2(2);
		});
		GButton msg3Btn = n0.GetChild("b3").asButton;
		msg3Btn.onClick.Add(() => {
			_OnMsg2(3);
		});
		GButton msg4Btn = n0.GetChild("b4").asButton;
		msg4Btn.onClick.Add(() => {
			_OnMsg2(4);
		});
		GButton msg5Btn = n0.GetChild("b5").asButton;
		msg5Btn.onClick.Add(() => {
			_OnMsg2(5);
		});
		GButton msg6Btn = n0.GetChild("b6").asButton;
		msg6Btn.onClick.Add(() => {
			_OnMsg2(6);
		});
		GButton msg7Btn = n0.GetChild("b7").asButton;
		msg7Btn.onClick.Add(() => {
			_OnMsg2(7);
		});
		GButton msg8Btn = n0.GetChild("b8").asButton;
		msg8Btn.onClick.Add(() => {
			_OnMsg2(8);
		});
		GButton msg9Btn = n0.GetChild("b9").asButton;
		msg9Btn.onClick.Add(() => {
			_OnMsg2(9);
		});

		// 初始化发送
		GButton sendBtn = uiComp.GetChild("发送按钮").asButton;
		sendBtn.onClick.Add(() => {
			_OnMsg(msgInput.text);
		});
	}

	void _OnMsg(string txt) {
		Hashtable reqArgs = new Hashtable ();
		reqArgs ["uid"] = DataKernel.inst.userInfo.uid;
		reqArgs ["roomId"] = GameDataKernel.inst.roomId;
		reqArgs ["message"] = "text://" + txt;

		NetIOController.inst.SendMessage (
			new IOMessage(
				IOCmds.CMSG_PLAYER_REQ_BROADCAST_MSG, 
				IOStates.STATE_OK, 
				new HashtableEx(reqArgs)));

		_Close ();
	}

	void _OnMsg2(int id) {
		Hashtable reqArgs = new Hashtable ();
		reqArgs ["uid"] = DataKernel.inst.userInfo.uid;
		reqArgs ["roomId"] = GameDataKernel.inst.roomId;
		reqArgs ["message"] = "fix://" + id;

		NetIOController.inst.SendMessage (
			new IOMessage(
				IOCmds.CMSG_PLAYER_REQ_BROADCAST_MSG, 
				IOStates.STATE_OK, 
				new HashtableEx(reqArgs)));

		_Close ();
	}

	public void _Close() {
		DebugInfo.Log("关闭聊天");

		AudioController.inst.PlayUIClickEffect();
		uiComp.RemoveFromParent();
		uiComp.Dispose();
		base.Close();
	}

	/// <summary>
	/// 弹出窗口
	/// </summary>
	public static void Popup()
	{
		DebugInfo.Log("打开聊天");
		UIRootController.inst.PopupWindow(new UIGameChat());
	}
}

